﻿using MediatR;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.COA
{
    /// <summary>
    /// 显示检验报告项目
    /// </summary>
    public class GetCPOPQCDataHandler : IRequestHandler<GetCPOPQCDataCommand, Result>
    {
        public GetCPOPQCDataHandler(IBaseRepository<IPQCGoodsData> iPQCGoodsDataRepository, IBaseRepository<IPQCRemarkData> iPQCRemarkDataRepository, IBaseRepository<PRQSData> pRQSDataRepository)
        {
            IPQCGoodsDataRepository = iPQCGoodsDataRepository;
            IPQCRemarkDataRepository = iPQCRemarkDataRepository;
            PRQSDataRepository = pRQSDataRepository;
        }

        public IBaseRepository<IPQCGoodsData> IPQCGoodsDataRepository { get; set; }
        public IBaseRepository<IPQCRemarkData> IPQCRemarkDataRepository { get; set; }
        public IBaseRepository<PRQSData> PRQSDataRepository { get; set; }
        public async Task<Result> Handle(GetCPOPQCDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            var list = (from a in IPQCGoodsDataRepository.GetAll()
                        join b in IPQCRemarkDataRepository.GetAll()
                        on a.IPQCId equals b.IPQCId
                        join c in PRQSDataRepository.GetAll()
                        on b.PRQSId equals c.PRQSId
                        where a.GoodsId == request.GoodsId && a.GoodsName == request.GoodsName
                        select new ShowCOPIPACDataDTO
                        {
                            PRQSId =c.PRQSId,
                            PRQSName =c.PRQSName,
                            PRQSindex =c.PRQSindex,
                            Require =c.Require
                        }
                      ).ToList();
            if (list != null)
            {
                result.Code = (int)CommonErrorCode.Success;
                result.Data = list;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
